package Controller;

import Bean.FileBean;
import Dao.FileSqlOperation;
import Dao.Impl.FileSqlOperationImpl;
import Utils.FileOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

@WebServlet("/download")
public class downloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException {
        String ServletFileName = request.getParameter("nameKey");
        System.out.println(ServletFileName);


        FileSqlOperation fileSqlOperation = new FileSqlOperationImpl(new FileOperation());
        FileBean fileBean = fileSqlOperation.downloadOneBynameKey(ServletFileName);

//        获取文件的数据类型
//        String  mimeType=servletContext.getMimeType(fileBean.getType());
//        response.setContentType("application/download");
        System.out.println("下载的文件类型：" + fileBean.getType() + "\tpath：" + fileBean.getRelativePath() + fileBean.getType());
        String encodingName= URLEncoder.encode(fileBean.getName(),"UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + encodingName);
        //文件的下载使用正常的文件输出流即可
        String mimeType=getServletContext().getMimeType(fileBean.getType());
        System.out.println("mimeType:"+mimeType);
        response.setHeader("Content-Type",mimeType);

/*      字节传输
        getResourceAsStream()是从目录下进行查找
        InputStream in = getServletContext().getResourceAsStream("/WEB-INF/stores/2022/08/24/387911352.png");
        InputStream in = getServletContext().getResourceAsStream(fileBean.getRelativePath()+fileBean.getType());
        功能的实现方法与IOUtils.copy是相同的，
        ServletOutputStream out = response.getOutputStream();
        byte[] buffer = new byte[1024*100];
        int len=0;
        while((len=in.read(buffer))!=-1){
           out.write(buffer, 0, len);
        }*/
/*      字符传输
        InputStream in = context.getResourceAsStream(fileBean.getRelativePath());
        InputStreamReader reader=new InputStreamReader(in);
        OutputStream out = response.getOutputStream();
        OutputStreamWriter writer=new OutputStreamWriter(out);
        System.out.println(in.read());
        char [] buffer=new char[1024*10];
        int len;
        while((len=reader.read(buffer))!=-1){
            writer.write(buffer, 0, len);
        }*/
//        将文件进行copy一份 采用apache.common.io 包中封装好的类
        ServletContext servletContext=getServletContext();
        InputStream resourcesAsStream =servletContext.getResourceAsStream(fileBean.getRelativePath());
        ServletOutputStream outputStream=response.getOutputStream();
        IOUtils.copy(resourcesAsStream,outputStream);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
